package com.tjetc.view.manage;

import com.tjetc.domain.Newborn;
import com.tjetc.domain.Vaccine;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.VaccineService;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

public class VaccineView {//疫苗管理
    private VaccineService vaccineService = ClientProxy.getProxyInstance(VaccineService.class);
    Scanner sc = new Scanner(System.in);
    public void start() {
        while (true) {
            try {
                System.out.println("1.添加疫苗");
                System.out.println("2.修改疫苗的信息");
                System.out.println("3.删除疫苗");
                System.out.println("4.查询库存少于某值的疫苗");
                System.out.println("5.查询某新生儿的接种情况");
                System.out.println("6.查询全部的疫苗信息");
                System.out.println("7.查询新生儿没有接种的疫苗");
                System.out.println("8.获取新生儿接种疫苗的列表(包含新生儿的id,姓名,以及接种的疫苗)");
                System.out.println("0.返回上一级");
                int choice = sc.nextInt();
                switch (choice) {
                    case 1:
                        addVaccine();
                        break;
                    case 2:
                        updateVaccine();
                        break;
                    case 3:
                        deleteVaccine();
                        break;
                    case 4:
                        getVaccineByQuantity();
                        break;
                    case 5:
                        getVaccineByNewBornId();
                        break;
                    case 6:
                        getVaccineList();
                        break;
                    case 7:
                        getVaccineNotByNewBornId();//获取新生儿没有接种的疫苗
                        break;
                    case 8:
                        getlistAll();//获取新生儿接种疫苗的列表(包含新生儿的id,姓名,以及接种的疫苗)
                    case 0:
                        return;
                }
            } catch (Exception e) {
                System.out.println("输入错误,请重新输入");
                sc.nextLine();
            }
        }

    }

    private void getlistAll() {
        List<Newborn> listAll = vaccineService.getlistAll();
        if (listAll.size()==0){
            System.out.println("没有接种疫苗表");
            return;
        }
        for (Newborn newborn : listAll) {
            System.out.println(newborn.getId()+" "+newborn.getName()+" "+newborn.getVaccineList());
        }
    }

    private void getVaccineByQuantity() {
        System.out.println("请输入一个数,输出库存数量小于该数的");
        int id = sc.nextInt();
        List<Vaccine> vaccineByQuantity = vaccineService.getVaccineByQuantity(id);
        for (Vaccine vaccine : vaccineByQuantity) {
            System.out.println(vaccine);
        }

    }

    public void getVaccineList() {
        if (vaccineService.getVaccineList()==null){//获取所有疫苗
            System.out.println("没有疫苗");
            return;
        }else{
            List<Vaccine> vaccineList = vaccineService.getVaccineList();
            for (Vaccine vaccine : vaccineList) {
                System.out.println(vaccine);
            }
        }
    }
    private void getVaccineByNewBornId() {//获取新生儿接种的所有的的疫苗
        System.out.println("请输入要查询的新生儿的id");
        int id = sc.nextInt();
        List<Vaccine> vaccineByNewBornId = vaccineService.getVaccineByNewBornId(id);
        if (vaccineByNewBornId.size()==0){
            System.out.println("没有疫苗");
            return;
        }
        for (Vaccine vaccine : vaccineByNewBornId) {
            System.out.println(vaccine);
        }
    }

    private void getVaccineNotByNewBornId() {//获取没有接种的疫苗
        System.out.println("请输入要查询的新生儿的id");
        int id = sc.nextInt();
        List<Vaccine> vaccineNotByNewBornId = vaccineService.getVaccineNotByNewBornId(id);
        if (vaccineNotByNewBornId.size()==0){
            System.out.println("没有没有接种的疫苗(接种的疫苗都接种了),出院喽");
            return;
        }
        System.out.println(vaccineNotByNewBornId);
        System.out.println("还需要进行接种");
    }

    private void deleteVaccine() {
        System.out.println("请输入想要删除的疫苗的id");
        int id = sc.nextInt();
        Result result = vaccineService.deleteVaccine(id);
        System.out.println(result.getMsg());
    }

    private void updateVaccine() {
        System.out.println("请输入疫苗的id");
        int id = sc.nextInt();
        System.out.println("请输入疫苗的名字");
        String name = sc.next();
        System.out.println("请输入疫苗的库存");
        int quantity = sc.nextInt();
        Vaccine vaccine = new Vaccine();
        vaccine.setId(id);
        vaccine.setName(name);
        vaccine.setQuantity(quantity);
        Result result = vaccineService.updateVaccine(vaccine);
        System.out.println(result.getMsg());

    }

    private void addVaccine() {
        System.out.println("请输入疫苗的名字");
        String name = sc.next();
        System.out.println("请输入疫苗的库存");
        int quantity = sc.nextInt();
        Vaccine vaccine = new Vaccine();
        vaccine.setName(name);
        vaccine.setQuantity(quantity);
        Result result = vaccineService.addVaccine(vaccine);
        System.out.println(result.getMsg());
    }

    public Object getVaccineByName() {//根据疫苗名查询
        System.out.println("请输入你想要查询的姓名");
        String name=sc.next();
        Vaccine vaccineByName = vaccineService.getVaccineByName(name);
        if(vaccineByName==null){
            System.out.println( "没有该疫苗");
            return null;
        }
        return vaccineByName;
    }

}
